#!/usr/bin/env python3

# apt-get install python3-dbus.mainloop.pyqt5

# from gi.repository import GLib
from PyQt5.QtCore import QCoreApplication

import dbus
from dbus.mainloop.pyqt5 import DBusQtMainLoop

import sys, subprocess

DBUS_Name = "com.app.i5os"
DBUS_Obj_Path = "/app"
DBUS_Interface = "com.app.i5os.registry"
DBUS_Signal = "switchapp"

class SignalListener:
    def __init__(self):
        bus = dbus.SessionBus()
        proxy = bus.get_object(DBUS_Name, DBUS_Obj_Path)
        proxy.connect_to_signal(DBUS_Signal),
                                self.Switch_Page,  # callback
                                dbus_interface=DBUS_Interface
                                # , arg0=""  # String argument matching
                                )

    def Switch_Page(self, message):
        print ("Received signal (by connecting using remote object) and it says: " + message)
        subprocess.call("zenity --info --text='response by PyQt5 signal listener: {}'".format(message), shell=True)


if __name__ == "__main__":
    app = QCoreApplication(sys.argv)

    DBusQtMainLoop(set_as_default=True)
    SignalListener()

    sys.exit(app.exec_())
